<?php

require_once(DAEMONE_HOME.'/lib/lib_database.php');

function get_rss_feed_source($where, $limit)
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT * from rss_feed_sources "
			." $where "
			." $limit ";
		
	$result = mysql_query($sql);
	
	mysql_close($connection);
	return $result;
}

function get_rss_feed_channel($where, $limit)
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT * from rss_feed_channel "
			." $where "
			." $limit ";
		
	$result = mysql_query($sql);
	
	mysql_close($connection);
	return $result;
}

function get_rss_feed_items($where, $order_by, $limit)
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT it.*, ct.category_name from rss_feed_item it "
		   ."INNER JOIN categories ct ON it.category_id=ct.category_id "
		   ." $where "
		   ." $order_by "
		   ." $limit ";
	
	$result = mysql_query($sql);
	
	mysql_close($connection);
	return $result;
}

function get_categories($where, $limit)
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT * from categories "
			." $where "
			." $limit ";
	$result = mysql_query($sql);
	
	mysql_close($connection);
	return $result;
}

function get_rss_feed_category($category_id)
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT * from categories "
		   ." WHERE category_id = $category_id ";
	$result = mysql_query($sql);
	
	mysql_close($connection);
	return $result;
}

function update_rss_feed_source($rss_feed_source_id, $status)
{
	global $config;
	$connection = get_connection();
	mysql_query("UPDATE rss_feed_sources SET grabbed_status = $status where rss_feed_source_id = $rss_feed_source_id ");
	
	mysql_close($connection);
}

function update_rss_feed_item_status($item_id, $wp_post_id, $status)
{
	global $config;
	$connection = get_connection();
	mysql_query("UPDATE rss_feed_item SET grabbed_status = $status , wp_post_id = $wp_post_id where item_id = $item_id ");
	
	mysql_close($connection);
}

function update_rss_feed_item_date($item_id, $date_string)
{
	global $config;
	$connection = get_connection();
	mysql_query("UPDATE rss_feed_item SET date_string = '$date_string' WHERE item_id = $item_id ");
	
	mysql_close($connection);
}

function update_rss_feed_category_status($category_id, $wp_category_id, $status)
{
	global $config;
	$connection = get_connection();
	mysql_query("UPDATE categories SET status = $status , wp_category_id = $wp_category_id WHERE category_id = $category_id ");
	
	mysql_close($connection);
}

function add_rss_channel($channel)
{
	global $config;
	$connection = get_connection();
	mysql_query("INSERT IGNORE INTO rss_feed_channel (title, description, author, link, guid, pubDate, rss_feed_source_id, category_id , rss_feed_grabed_on )"
		." VALUES ("
		."'$channel[title]', '$channel[description]'"
		.",'".(isset($channel['author']) ? $channel['author'] : '')."'"
		.",'$channel[link]'"
		.",'".(isset($channel['guid']) ? $channel['guid']:'')."'"
		.",'".(isset($channel['pubdate']) ? $channel['pubdate']:'')."'"
		.",'".(isset($channel['rss_feed_source_id']) ? $channel['rss_feed_source_id']:'')."'"
		.",'".(isset($channel['category_id']) ? $channel['category_id']:'')."'"
		.", NOW()"
		.")");

	$channel_id = mysql_insert_id();
	mysql_close($connection);
	return $channel_id;
}

function add_rss_item(&$item)
{
	global $config;
	$connection = get_connection();
	$sql = "INSERT IGNORE INTO rss_feed_item (channel_id, title, title_slug, description, author, link, guid, pubDate, rss_feed_source_id, category_id, rss_feed_grabed_on, category_name, parent_category_name, html_meta_keywords  )"
		." VALUES ("
		."'$item[channel_id]'"
		.",'".addslashes($item['title'])."','".strtolower(addslashes(preg_replace('/[\W]/', '-', $item['title']))) ."','".addslashes($item['description'])."'"
		.",'".addslashes(isset($item['author']) ? $item['author'] : '')."'"
		.",'".addslashes($item['link'])."'"
		.",'".addslashes(isset($item['guid']) ? $item['guid'] : '')."'"
		.",'".(isset($item['pubdate']) ? $item['pubdate'] : '')."'"
		.",'".(isset($item['rss_feed_source_id']) ? $item['rss_feed_source_id'] : '')."'"
		.",'".(isset($item['category_id']) ? $item['category_id'] : '')."'"
		.", now()"
		.",'".(isset($item['category_name']) ? addslashes($item['category_name']) : '')."'"
		.",'".(isset($item['parent_category_name']) ? addslashes($item['parent_category_name']) : '')."'"
		.",'".(isset($item['category_name']) ? addslashes(str_replace('-', ' ',$item['category_name'])) : '')."'"
		.")";
		
	mysql_query($sql);
	$item_id = mysql_insert_id();
	mysql_close($connection);
	return $item_id;
}

function add_rss_feed_grabbed_history(&$item)
{
	global $config;
	$connection = get_connection();
	
	$sql = "INSERT IGNORE INTO rss_feed_grab_history (rss_feed_source_id, channel_id, rss_feed_grabbed_on, rss_channel_total_item_counter, rss_channel_new_item_counter)"
		." VALUES ( "
		."'".(isset($item['rss_feed_source_id']) ? $item['rss_feed_source_id'] : '')."'"
		.",'$item[channel_id]'"
		.", now()"
		.",'".(isset($item['rss_channel_total_item_counter']) ? $item['rss_channel_total_item_counter'] : '0')."'"
		.",'".(isset($item['rss_channel_new_item_counter']) ? $item['rss_channel_new_item_counter'] : '0')."'"
		.")";
		
	mysql_query($sql);
	mysql_close($connection);
}
function update_rss_item_pub_date ($where)
{
	global $config;
	$connection = get_connection();
	$sql = " UPDATE rss_feed_item SET pubdatetime = str_to_date(mid(pubdate,1, 25),'%a, %d %b %Y %H:%i:%s')  $where " ;
	mysql_query($sql);
	mysql_close($connection);
}

function update_rss_feed_item ($rss_feed_item_id, $data)
{
	global $config;
	$connection = get_connection();
	
	$sql = " UPDATE rss_feed_item  SET " ;
	$set_clause = '';
	foreach ($data as $key => $value)
    {
		$set_clause .= ", $key = '".addslashes($value)."' ";
	}
	$set_clause = substr($set_clause, 2);
	$sql = $sql . $set_clause . " where item_id=$rss_feed_item_id"; 
	 
	$result = mysql_query($sql);
	if ($result === FALSE) echo mysql_error();
	mysql_close($connection);
}

function update_rss_feed_item_related_article ($rss_feed_item_id, $data)
{
	if (count($data) == 0)
		return true;
		
	global $config;
	$connection = get_connection();
	
	$sql = " insert ignore into rss_feed_item_related_article (rss_feed_item_id, related_article_url) values  " ;
	
	foreach ($data as $item)
	{
		$values .= ", ('". $rss_feed_item_id. "','" . addslashes($item['related_article_url']) . "')";
		// echo  "related article : link ${item['related_article_url']} <br/> \r\n ";
	}
	$values = substr($values, 2);
	$sql = $sql . $values; 
	
	
	$result = mysql_query($sql);
	
	if ($result === FALSE) echo mysql_error();
	mysql_close($connection);
}

function get_rss_grab_full_article_signal()
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT signal from config where process_key='rss_grab_full_article'";		
	$result = mysql_query($sql);
	mysql_close($connection);
	if($result === FALSE || mysql_num_rows($result) == 0) 
	{
		echo 'Error : rss_grab_full_article signal : ', mysql_error() ;
		$signal = '';
	}
	else 
	{
		$row = mysql_fetch_assoc($result); 
		$signal = $row['signal'];
	}
	
	return $signal;
}

function get_rss_grab_full_article_status()
{
	global $config;
	$connection = get_connection();
	$sql = "SELECT status from config where process_key='rss_grab_full_article'";		
	$result = mysql_query($sql);
	mysql_close($connection);
	if($result === FALSE || mysql_num_rows($result) == 0) 
	{
		echo 'Error : rss_grab_full_article status : ', mysql_error() ;
		$status = '';
	}
	else 
	{
		$row = mysql_fetch_assoc($result); 
		$status = $row['status'];
	}
	
	return $status;
}
?>
